package com.grablist.feihu.grablist.ui.actviity.login.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;

import javax.inject.Inject;

import com.grablist.feihu.grablist.base.BaseBiz;
import com.grablist.feihu.grablist.bean.UserInfo;
import com.grablist.feihu.grablist.comm.Constants;
import com.grablist.feihu.grablist.comm.MyApplication;
import com.grablist.feihu.grablist.ui.actviity.MainActivity;
import com.grablist.feihu.grablist.ui.actviity.login.SelcetInfoAct;
import com.grablist.feihu.grablist.ui.actviity.login.biz.LoginBiz;
import com.grablist.feihu.grablist.ui.actviity.login.contract.LoginContract;
import com.grablist.feihu.grablist.ui.actviity.user.ResetPwdAct;
import com.grablist.feihu.grablist.util.PreferencesUtils;
import com.grablist.feihu.grablist.util.ValidatorUtil;

/**
 * Created by Administrator on 2017/10/25.
 */

public class LoginPresenter implements LoginContract.LoginPresenter {

    LoginContract.View view;
    LoginBiz loginBiz;

    @Inject
    public LoginPresenter(LoginContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        loginBiz = (LoginBiz) biz;
    }


    @Override
    public void login(String mobile, final String password, final String alias, final boolean isSave) {
        if (TextUtils.isEmpty(mobile)) {
            view.showMsg("手机号未填写");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            view.showMsg("密码未填写");
            return;
        }
        view.loading(true);
        loginBiz.Login(mobile, password, alias, "android", new BaseBiz.Callback<UserInfo>() {
            @Override
            public void onSuccess(UserInfo info) {
                if (isSave) {
                    info.setSave(true);
                }
                info.setAlias(alias);
                info.setPassword(password);
                PreferencesUtils.setStringPreferences("UserInfo",
                        "UserInfo",
                        new Gson().toJson(info));
                MyApplication.userInfo = info;
                view.startAct(MainActivity.class, null);
                view.loading(false);
                Constants.registerActivity.clear();
                view.finishAct();
            }

            @Override
            public void onFailure(UserInfo info) {
                view.loading(false);
            }
        });
    }


    @Override
    public void showLoginOrRegister(boolean isshow) {
        view.showLayout(isshow);
    }

    @Override
    public void getCode(String phone) {
        if (TextUtils.isEmpty(phone)) {
            view.showMsg("手机号未填写");
            return;
        }
        if (!ValidatorUtil.isMobile(phone)) {
            view.showMsg("手机号码不正确");
            return;
        }
        view.loading(true);
        loginBiz.mobileSms(phone, 1, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                view.countDown();
                view.loading(false);
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void register(String phone, final String password, String vaidCode,final String alias) {
        if (TextUtils.isEmpty(phone)) {
            view.showMsg("手机号未填写");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            view.showMsg("密码未填写");
            return;
        }
        if (TextUtils.isEmpty(vaidCode)) {
            view.showMsg("验证码未填写");
            return;
        }
        if (!ValidatorUtil.isMobile(phone)) {
            view.showMsg("手机号码不正确");
            return;
        }
        view.loading(true);
        loginBiz.regCheck(phone, password, vaidCode, alias, new BaseBiz.Callback<UserInfo>() {
            @Override
            public void onSuccess(UserInfo info) {
                view.countDown();
                info.setAlias(alias);
                PreferencesUtils.setStringPreferences("UserInfo",
                        "UserInfo",
                        new Gson().toJson(info));
                MyApplication.userInfo = info;
                view.loading(false);
                view.startActString(SelcetInfoAct.class,"data");
            }

            @Override
            public void onFailure(UserInfo info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void toForget() {
        view.startAct(ResetPwdAct.class, null);

    }

    @Override
    public void showPwd(int type, boolean isshow) {
        view.showPwdType(type, isshow);
    }
}
